#include "inputhandler.h"


// The constructor that is called
// each time an object of this class
// is created
inputHandler::inputHandler()
{
	leftPressed = false;
	rightPressed = false;
	upPressed = false;
	downPressed = false;
	actionPressed = false;
	talkPressed = false;
	escPressed = false;
}

// This method will do the handling
// of the input and marking key states
void inputHandler::doInput(sf::RenderWindow &window)
{
	// Do the checking of non-key press events
	// such as clicking the [x] on the window to
	// close the game, or minimize/maximize, etc
	while (window.pollEvent(eventHandler))
	{
		if (eventHandler.type == sf::Event::Closed)
		{
			window.close();
		}
	}

	// Set everything to false again and check
	// if the keys are still pressed or not
	leftPressed = false;
	rightPressed = false;
	upPressed = false;
	downPressed = false;
	actionPressed = false;
	talkPressed = false;
	escPressed = false;

	// Check if any of the keys are pressed or
	// not, if yes, mark them as pressed
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
	{
		leftPressed = true;
	}
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
	{
		rightPressed = true;
	}
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
	{
		upPressed = true;
	}
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
	{
		downPressed = true;
	}
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::X))
	{
		actionPressed = true;
	}
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
	{
		talkPressed = true;
	}
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
	{
		jumpPressed = true;
	}
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
	{
		escPressed = true;
	}
}

// This function will return whether
// the requested key was pressed or not
bool inputHandler::isPressed(int id)
{
	switch(id)
	{
		case BTN_LEFT:
		{
			return leftPressed;
		}
		case BTN_RIGHT:
		{
			return rightPressed;
		}
		case BTN_UP:
		{
			return upPressed;
		}
		case BTN_DOWN:
		{
			return downPressed;
		}
		case BTN_ACTION:
		{
			return actionPressed;
		}
		case BTN_TALK:
		{
			return talkPressed;
		}
		case BTN_JUMP:
		{
			return jumpPressed;
		}
		case BTN_ENTER:
		{
			return enterPressed;
		}
		case BTN_ESC:
		{
			return escPressed;
		}
		default:
		{
			printf("[x] inputHandler :: Illegal Key ID state requested (id:%d)\n", id);
			break;
		}

		// NOTE:
		// The reason I'm not using "break" above, is because
		// I'm instead 'returning' true or false, so the code
		// will quit there instead of be able to continue
	}

	// Default exit, if none of the above
	// keys states are requested
	return false;
}